package cj.web.portal.controllers;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.constants.KDAddressAreaEnum;
import cj.web.admin.constants.KDAddressStreetEnum;
import cj.web.admin.constants.KDBookingStatusEnum;
import cj.web.admin.constants.KDInTypeEnum;
import cj.web.admin.constants.KDRecovePaperWeightEnum;
import cj.web.portal.dao.KDBookingDAO;

@Path("kdwx")
public class KDWXController {

	private static Logger logger = Logger.getLogger(KDWXController.class.getName());
	
	private static final int wuhanCity = 0;
	
	@Autowired
	private KDBookingDAO kdBookingDAO;
	
	/**
	 * 订报卡启报
	 * @param model
	 * @return
	 */
	@Get("bookingcard")
	public String showBookingCard(Model model)
	{
		model.add("currentDate", DateUtils.getNowTime(DateUtils.DATE_SMALL_STR));
		model.add("kdAddressAreaEnum", EnumSet.allOf(KDAddressAreaEnum.class));
		model.add("kdAddressStreetEnum", EnumSet.allOf(KDAddressStreetEnum.class));
		return "org/kdgs/wx/bookingCard.vm";
	}
	
	/**
	 * 启报
	 * @param cardNum	启报卡
	 * @param cardPwd	密码
	 * @param link		联系人
	 * @param phone		联系电话
	 * @param area		区域
	 * @param street	街道
	 * @param address	详细地址
	 * @param beginAt	起始时间
	 * @return
	 */
	@Post("submitBookingCard")
	public String submitBookingCard(@Param("cardNum")String cardNum, @Param("cardPwd")String cardPwd, @Param("link")String link, 
			@Param("phone")String phone, @Param("city_picker") String city_picker, @Param("address")String address, @Param("beginAt")String beginAt){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String area = city_picker.split(" ")[0];
		String street = city_picker.split(" ")[1];
		
		int areaVal = KDAddressAreaEnum.getName(area);
		int streetVal = KDAddressStreetEnum.getName(street);
		
		List<Map> maps = kdBookingDAO.validateCard(cardNum);
		if(maps != null && maps.size() > 0){
			String pwd = maps.get(0).get("cardPwd")==null ? "" : maps.get(0).get("cardPwd").toString();
			if("".equals(pwd)){
				kdBookingDAO.saveBookingCard(KDInTypeEnum.WX.getValue(),cardNum,cardPwd,link,phone,wuhanCity,areaVal,streetVal,address,beginAt);
				kdBookingDAO.updateStatus(cardNum);
			}else{
				if(pwd.equals(cardPwd)){
					kdBookingDAO.saveBookingCard(KDInTypeEnum.WX.getValue(),cardNum,cardPwd,link,phone,wuhanCity,areaVal,streetVal,address,beginAt);
					kdBookingDAO.updateStatus(cardNum);
				}
				else{
					jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
				}
			}
			
			
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 验证启报卡卡号
	 * @param cardNum
	 * @return
	 */
	@Post("checkCardNum")
	public String checkCardNum(@Param("cardNum")String cardNum)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> maps = kdBookingDAO.validateCardNoStatus(cardNum);
		
		if(maps!=null && maps.size()>0){
			jo.put("bzzl", maps.get(0).get("paperName"));
			jo.put("bkmc", maps.get(0).get("paperCardName"));
			jo.put("months", maps.get(0).get("months"));
			jo.put("status", maps.get(0).get("status"));
		}else{
			jo.put("bzzl", "");
			jo.put("bkmc", "");
		}
		
		return "@" + jo.toString();
	}
	
	
	/**
	 * 旧报回收
	 * @param model
	 * @return
	 */
	@Get("oldpapersrec")
	public String showOldPapersRec(Model model)
	{
		model.add("kdAddressAreaEnum", EnumSet.allOf(KDAddressAreaEnum.class));
		model.add("kdAddressStreetEnum", EnumSet.allOf(KDAddressStreetEnum.class));
		model.add("KDRecovePaperWeightEnum", EnumSet.allOf(KDRecovePaperWeightEnum.class));
		return "org/kdgs/wx/oldPaperRec.vm";
	}
	
	/**
	 * 保存旧报回收
	 * @param link		联系人
	 * @param phone		联系电话
	 * @param city		市
	 * @param area		区
	 * @param street	街道
	 * @param address	地址
	 * @return
	 */
	@Post("saveOldBooking")
	public String saveOldBooking(@Param("link") String link,@Param("phone") String phone,@Param("address") String address,
			@Param("weight") String weight,@Param("descs") String descs,@Param("city_picker") String city_picker,
			@Param("type") String type,@Param("category") String category)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		System.out.println(city_picker);
		
		String area = city_picker.split(" ")[0];
		String street = city_picker.split(" ")[1];
		try {
			Map rep = new HashMap();
			rep.put("inType", KDInTypeEnum.WX.getValue());
			rep.put("link", link);
			rep.put("phone", phone);
			rep.put("city", wuhanCity);
			rep.put("area", KDAddressAreaEnum.getName(area));
			rep.put("street", KDAddressStreetEnum.getName(street));
			rep.put("address", address);
			rep.put("descs", descs);
			rep.put("weight", weight);
			rep.put("status", KDBookingStatusEnum.DCL.getValue());
			rep.put("deleteFlag", Constants.deleteFlag.deleteFlag_1);
			rep.put("createUser", "portal");
			rep.put("updateUser", "portal");
			rep.put("type", type);
			rep.put("category", category);
			
			kdBookingDAO.createKDOldBooking(rep);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 投诉建议
	 * @param model
	 * @return
	 */
	@Get("tousu")
	public String showTouSu(Model model)
	{
		return "org/kdgs/wx/touSu.vm";
	}
	
	/**
	 * 投诉建议
	 * @param msgBox	消息
	 * @param link		联系人
	 * @param phone		联系电话
	 * @return
	 */
	@Post("saveAdvise")
	public String saveAdvise(@Param("msgBox") String msgBox,@Param("link") String link,@Param("phone") String phone)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			Map rep = new HashMap();
			rep.put("inType", KDInTypeEnum.WX.getValue());
			rep.put("link", link);
			rep.put("phone", phone);
			rep.put("msg", msgBox);
			rep.put("status", KDBookingStatusEnum.DCL.getValue());
			rep.put("deleteFlag", Constants.deleteFlag.deleteFlag_1);
			rep.put("createUser", "portal");
			rep.put("updateUser", "portal");
			
			kdBookingDAO.createKDAdvise(rep);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
}
